package top.hcode.hoj;

import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import org.nutz.lang.Strings;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.pojo.entity.org.TyOrg;
import top.hcode.hoj.utils.CommonMethods;
import top.hcode.hoj.utils.Constants;

import java.io.File;
import java.util.Date;

/**
 * @author orangej
 * @since 2025/1/24
 */
public class ImportSchoolApp extends BaseDbApp {
    public static void main(String[] args) {
        LazyDao dao = buildLazyDao();
        Date now = new Date();
        int treeCode = 1002;

        // 从 d:/school.csv 读取学校信息，第一个字段为 orgId，接着是 orgName
        CsvData data = CsvUtil.getReader().read(new File("d:/school.csv"));
        int rowCount = data.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            CsvRow row = data.getRow(i);
            String orgId = row.get(0);
            String orgName = row.get(1);
            if (Strings.isBlank(orgId)) {
                continue;
            }

            TyOrg org = dao.selectOne(TyOrg.class, "select * from ty_org where id = ?", orgId);
            if (org == null) {
                org = new TyOrg();
                org.setId(orgId);
                org.setOrgName(orgName);
                org.setGmtCreate(now);
                org.setGmtModified(now);
                org.setOrgType(Constants.OrgType.SCHOOL.getType());
                org.setTreeCode(String.valueOf((++treeCode)));

                dao.insert("ty_org", CommonMethods.obj2HumpMap(org));
            }
        }
    }
}
